<?php
/**
 * Customer Controller is generaly for the gather customer inquery 
 * which is in the company info page first page.
 * 
 * @author Norman
 *
 */

class CustomerController extends Zend_Controller_Action
{
	public function indexAction(){
		
	}
	/**
	 * this is the small block in the company info page
	 */
	public function inqueryAction(){
		
		$this->view->shopHead = $this->_getParam("shop");
		
	}
	/**
	 * this is actual page to submit and save the inquery into database
	 */
	public function saveInqueryAction(){
		
		$shopHead = $_POST["shop_head"];
		$dateToday = Model_DatetimeHelper::dateToday();
		$timeNow = Model_DatetimeHelper::timeNow();
		$inq = new Model_DbTable_Customer_Inq();
		$idInq = 0;
		
		if($_POST){

			$idInq = $inq->addInq($_POST["shop_head"],$_POST["content"],$dateToday,$timeNow,$_POST["mymenu"]);			
		}
		
		$this->view->idInq = $idInq;
		$this->view->shopHead = $shopHead;
		
		//$this->_redirect("/customer/inquery/shop/".trim($_POST["shop_head"]));
		
	}
	/**
	 * after staff submit the inquery, if the staff like 
	 * they can save their password , so the HQ know who is uploading the inquery
	 */
	public function saveStaffAction(){
		
		$shopHead = $this->_getParam("shop");
		$this->view->shopHead = $shopHead;
		$this->view->errormessage = "";
		
		$idInq = 0;
		
		$inq = new Model_DbTable_Customer_Inq();
		$attn = new Model_DbTable_Roster_Stafflogindetail();
		
		if($_POST){
			
			$idInq = $_POST["inq_id"];
			$idStaff =$attn->checkPasswordCorrect(Model_EncryptHelper::hashsha($_POST["password"]));

			if($idStaff){
				$inq->updateStaffName($idInq, $idStaff["id"]);
				$this->_redirect("/customer/inquery/shop/".$shopHead);
			}
			else{
				$this->view->errormessage = "Password incorrect!!<br />";
			}
			
		}
		$this->view->idInq = $idInq;
		
	}
	/**
	 * Sending inquery function will run on eveyr monday lunch time 
	 */
	public function sendingInqAction(){
		
		$email = $this->_getParam("email");
		
		$custInq = new Model_DbTable_Customer_Inq();
		$stDetail = new Model_DbTable_Roster_Stafflogindetail();
		$dateLastMonday = Model_DatetimeHelper::getLastWeekMonday();
		$dateToday = Model_DatetimeHelper::dateToday();
		
		$dateBegin = $this->_getParam("date");
		//$dateEnd = "";
		if($dateBegin!=""){
			
			$dateLastMonday = $dateBegin;
			$dateToday = Model_DatetimeHelper::adjustDays("add", $dateLastMonday,6);
		}
	
		$shopRanks = $custInq->shopRankDate($dateLastMonday, $dateToday);  
		$detail = $custInq->listInqByDate($dateLastMonday, $dateToday);  
		
		//var_dump($shopRanks,$detail);
		
		$sh = new Model_DbTable_Shoplocation();
		$sList = $sh->listHeadArray();
		
		
		
		$strRank = "<table width='300' border = '1' cellpandding='10' ><tr><td>Shop</td><td>Qty</td></tr>";
		
		foreach($shopRanks as $shop){
			$strRank .="<tr>";
			$strRank .="<td>".$shop["shop_head"]."</td><td>".$shop["count"]."</td>";
			$strRank .="<tr>";
			//echo "<pre>";
			//var_dump($sList);
			//echo "</pre>";	
			$key = array_search($shop["shop_head"],$sList);
			$sList[$key] = "";
		}
		
		foreach($sList as $shopNo){
			
			if($shopNo!=""){
			$strRank .="<tr>";
			$strRank .="<td bgcolor ='red'>".$shopNo."</td><td>0</td>";
			$strRank .="<tr>";			
			}
		}
		
		
		
		$strRank .="</table>";
		
		$this->view->shopRank = $strRank;
		
		$strDetail = "<table width='500' border = '1' cellpandding='10' ><tr><td>Shop</td><td>Inq</td><td>date</td><td>Staff</td></tr>";
		foreach($detail as $det){
			$strDetail .="<tr>";
			$strDetail  .="<td>".$det["shop_head"]."</td>";
			$strCustInq = $det["content_inq"];
			$strCustInq = str_replace("Brand+Model+Product Type","", $strCustInq);
			$strCustInq = str_replace("[Or Price Review]","", $strCustInq);
			$strCustInq = str_replace("Barcode + Prefer Price","", $strCustInq);
			
			$strDetail .="<td>".trim($strCustInq)."</td>";
			$strDetail .="<td>".$det["date_inq"]."</td>";
			$StFullName="N/A";
			$stNickName="N/A";
			if($det["id_staff"]!=""){
			
			//var_dump($det["id_staff"]);	
			$stName = $stDetail->getDetail((int)$det["id_staff"]);
			//$StName = $stDetail->getDetail($det["id_stff"]);
			$StFullName = ucwords(Model_EncryptHelper::getSslpassword($stName["n"]));
			$stNickName = ucwords($stName["ni"]);
			}
			$strDetail .="<td>".$stNickName."(".$StFullName.")"."</td>";
			$strDetail  .="<tr>";			
			
		} 
		$strDetail  .="</table>";	
		
		$this->view->shopDetail = $strDetail;
		
		if($email == "yes"){
			
			$mail = new Model_Emailshandler();
			$subject = "Customer Inquiry From:".$dateLastMonday." TO ".$dateToday;
			$mailbody = $strDetail.$strRank;			
			$mail->sendNormalEmail("admin@phonecollection.com.au", $subject, $mailbody);
			$mail->sendNormalEmail("office@phonecollection.com.au", $subject, $mailbody);
			$mail->sendNormalEmail("phonecollection.au@gmail.com", $subject, $mailbody);
			
		}
	}
	

	/**
	 * this function is for warehouse to provide the feedback for the staff inquery 
	 * it is a que system that both china and austrlian can answer the question 
	 * however this system is not in good use 
	 * 
	 */
	public function feedbackAction(){
		
		$dateEnd = Model_DatetimeHelper::dateToday();
		$dateBegin7Days = Model_DatetimeHelper::adjustDays("sub",$dateEnd,7);
		$dateEnd8Days = Model_DatetimeHelper::adjustDays("sub", $dateEnd,8);
		
		$dateBegin = Model_DatetimeHelper::adjustWeeks("sub",Model_DatetimeHelper::getThisWeekMonday(), 6);		
		$inq = new Model_DbTable_Customer_Inq();

		
		$this->view->cwList = $inq->listInqByDate($dateBegin7Days, $dateEnd);
		$this->view->oldList = $inq->listInqNotComplete($dateBegin, $dateEnd8Days);
		
		$this->renderScript('/customer/feedback141029.phtml');
		//d($this->view->cwList,$this->view->oldList);
		if($_POST){
			d($_POST);
			foreach($_POST['feedback_au'] as $k => $v){
				if($v!=$_POST['org_choice'][$k]){
					echo $k." will update ";
					
				}
				// if the feedback changeed , if changed record otherwise just ignore 
			//	$inq->updateAuChoice($k, $v, $_POST['cmt_au'][$k]);
					
			}
		}	
	}

	
	/**
	 * this is part of the feedback system that was in china , if china can not get the stock 
	 * the shop staff should also been responsed, no need HQ to be in the middle
	 */
	public function feedbackChinaAction(){
		
		// 6 weeks non handled
		$dateEnd = Model_DatetimeHelper::dateToday();
		$dateBegin = Model_DatetimeHelper::adjustWeeks("sub",Model_DatetimeHelper::getThisWeekMonday(), 6);
		$inq = new Model_DbTable_Customer_Inq();
		$nonHandleList = $inq->listInqNonHandled($dateBegin,$dateEnd);
		$dateBeginCw = Model_DatetimeHelper::getThisWeekMonday();
		
		$dateBeginLw = Model_DatetimeHelper::getLastWeekMonday();
		$dateEndLw = Model_DatetimeHelper::getLastWeekSunday();
		
		$dateBegin2w = Model_DatetimeHelper::adjustWeeks("sub", $dateBeginLw,1);
		$dateEnd2w = Model_DatetimeHelper::adjustWeeks("sub", $dateEndLw, 1);
		
		$dateBegin3w = Model_DatetimeHelper::adjustWeeks("sub", $dateBeginLw,2);
		$dateEnd3w = Model_DatetimeHelper::adjustWeeks("sub", $dateEndLw, 2);
		
		
		
		if($_POST){
		
		
				//d($_POST);
				foreach($_POST['feedback_cn'] as $k => $v){
					$inq->updateCnChoice($k, $v, $_POST['cmt_cn'][$k], $_POST['eta_au'][$k]);
				}
		
	
				
		}
		
		
		$this->view->inqList = $nonHandleList;
		
		$this->view->cwList = $inq->listInqByDate($dateBeginCw, $dateEnd); // current week
		$this->view->lwList = $inq->listInqNotComplete($dateBeginLw, $dateEndLw); // last week
		$this->view->w2List = $inq->listInqNotComplete($dateBegin2w,$dateEnd2w);
		$this->view->w3List = $inq->listInqNotComplete($dateBegin3w,$dateEnd3w);
		$this->view->wngList = $inq->willNotGet($dateBegin, $dateEnd);
		$this->view->shipList = $inq->shipped($dateBegin, $dateEnd);
			
		
	}
	/**
	 * this is the one to monitoring the feedback status , see if the feedback is provided on time
	 * the function is there and should be automaticlly run
	 * 
	 */
	public function feedbackEmailAction(){
		
		$intDate = Model_DatetimeHelper::tranferToInt(Model_DatetimeHelper::dateToday());
		
		$inq = new Model_DbTable_Customer_Inq();
		
		$dateBegin6W = Model_DatetimeHelper::adjustWeeks("sub", Model_DatetimeHelper::getLastWeekMonday(), 6);
		$dateEndToday = Model_DatetimeHelper::dateToday();
		$dateEnd7Days = Model_DatetimeHelper::adjustDays("sub",Model_DatetimeHelper::dateToday(),7);
		// get all unhandled 
		$dateBegin2W = Model_DatetimeHelper::adjustWeeks("sub", Model_DatetimeHelper::getLastWeekMonday(), 4);
		$email = new Model_Emailshandler();
		
		$uhList = $inq->listInqNonHandled($dateBegin6W, $dateEnd7Days);
		$inCompleteList = $inq->listInqNotComplete($dateBegin2W, $dateEndToday);
		
		$arrChina = array();
		$arrKen = array();
		$arrAnother = array();
		
		
		if(!empty($uhList)){
			
			// send email of 7 days not handled 
		}
		if(!empty($inCompleteList)){
			
			$arrChina = array();
			$arrKen = array();
			$arrAnother = array();
			
			// waiting for other shop will wait for ever ? for now yes
			foreach($inCompleteList as $k => $v){
				
				$auFeedbackDate = $v['feedback_date_au'];
				
				if(Model_DatetimeHelper::dateDiff($dateEndToday, $auFeedbackDate) > 7){
					if($v['feedback_au'] == 1) { // send to china
						
						$arrChina[] = $v;
					}
				if($v['feedback_au'] == 21) { // ken's decision 
						
						$arrKen = $v;
					}
					
				}
				// 14 days 
				if( $v['feedback_au'] == 41  && Model_DatetimeHelper::dateDiff($dateEndToday, $v['date_inq']) > 14){
					
					$arrAnother[] = $v;
				}
				
			
			}
			
			d($arrChina,$arrKen,$arrAnother);
			
			if(!empty($arrChina)){
				$email->sendNormalEmail("eco3@phonecollection.com.au","send to china and china no response", "-----");
			}
			if(!empty($arrKen)){
				$email->sendNormalEmail("eco3@phonecollection.com.au","Jeffrey Please Remind Ken Again", "-----");
			}
			if(!empty($arrAnother)){
				$email->sendNormalEmail("eco3@phonecollection.com.au","Has been wait for 14 days", "-----");
			}
			
			//d($inCompleteList);
			// if it is send to china ->  larget then 7 days 
			// wait another 7 days and more then 14 days from the inital -> send email 
			
		}
		
		
		
		
		//source china ->ye 7 days
		//ken decision ->reminder in 2 days
		
		//7 day au
		// 14 day total au 
		
		//7 day china 
		
		
	}
	/**
	 * this is the function for shop to check there inquerys and feedback
	 * this page is not in good use
	 */
	
	public function checkFeedbackAction(){
		
		
		$dateBegin = Model_DatetimeHelper::getLastWeekMonday();
		$dateEnd = Model_DatetimeHelper::dateToday();
		
		$dateBegin6w = Model_DatetimeHelper::adjustWeeks("sub",Model_DatetimeHelper::getLastWeekMonday(), 5);
		$dateEnd6w = Model_DatetimeHelper::getLastWeekSunday();
		
		$shop = $this->_getParam("shop");
		
		$inq = new Model_DbTable_Customer_Inq();
		
		$inqYourShop = $inq->listInqByShopByDate($shop, $dateBegin, $dateEnd); // your feedback from last week to today
		$inqAll = $inq->listInqByDate($dateBegin6w, $dateEnd6w);
		
		$this->view->inqYourShop = $inqYourShop;
		$this->view->inqAll = $inqAll;
		
		// All shop's requirement in 6 weeks with Feedback of au and comment of au 
		
		
	}
}

?>